package com.zgkxzx.java;

import com.zgkxzx.java.bean.Person;
import com.zgkxzx.数据结构.DataStructTest;
import com.zgkxzx.算法.AgloTest;
import com.zgkxzx.数据结构.链表.ZLinkList;
import com.zgkxzx.java.mutilThread.forkAndJoin.Product;
import com.zgkxzx.java.mutilThread.forkAndJoin.ProductFactory;
import com.zgkxzx.java.mutilThread.forkAndJoin.ProductJoinTask;
import com.zgkxzx.设计模式.DisgnModeTest;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 创建者 zgkxzx
 * 日期   12/19/17
 * 描述
 */

public class JavaTest {

    public static void main(String[] args) throws UnsupportedEncodingException {

//        MyThread myThread = new MyThread("oo");
//        new Thread(myThread, "1-").start();
//        new Thread(myThread, "2-").start();

       /* int 最大公约数 = 最大公约数(13, 21);
        System.out.println("最大公约数 " + 最大公约数);

        int 最小公倍数 = 最小公倍数(13, 21);
        System.out.println("最小公倍数 " + 最小公倍数);*/

        //testCharset();
        /*binaryTreeTest();*/


        //最大子序列
        /*int calc = MaxChildSequece.calc("android", "random");
        System.out.println("最大子序列 " + calc);


        String s = BigNumber.multiOperate("9999999999", "123456789");
        System.out.println("大数相乘计算的结果 = " + s);*/


        //约瑟夫环
        /*Yephus yephus = new Yephus();
        yephus.init(10, 5);
        //yephus.run();

        int count = Rabit.getCount(5);
        System.out.println("兔子 = " + count);

        Scanner scanner = new Scanner(System.in);
        long l = scanner.nextLong();
        System.out.print("输入 " + l);*/

        //内部类的实例化
        Person person = new Person("d");

        Person.Foot foot = person.new Foot();

        foot.setWidth(10);


        //Fork/Join思想

        List<Product> products = ProductFactory.generate(10);

        //ProductTask productTask = new ProductTask(products, 0, products.size(), 0.2);
        ProductJoinTask productTask = new ProductJoinTask(0, 199);

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        forkJoinPool.execute(productTask);

        do {
            System.out.println(" PoolSize " + forkJoinPool.getPoolSize());
            System.out.println(" ActiveThreadCount " + forkJoinPool.getActiveThreadCount());
            System.out.println(" StealCount " + forkJoinPool.getStealCount());
            System.out.println(" Parallelism " + forkJoinPool.getParallelism());

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (!productTask.isDone());
        forkJoinPool.shutdown();

        if (productTask.isCompletedNormally()) {
            System.out.println(" productTask.isCompletedNormally ");
        }

//        for (Product product : products)
//            System.out.println(" product-> " + product);

        try {
            System.out.println(" productTask.get()-> " + productTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        //DCL测试
        //DCLTest.test();

//        String s1 = "http://192.168.0.184:8080/appstore/TPD/s/s.do?k=242a3b&age=12";
//
//        String encode = URLEncoder.encode(s1, "UTF-8");
//
//        System.out.println("encode " + encode);
//
//        String s = "http://192.168.0.184:8080/appstore/TPD/s/s.do?k=242a3b&a=MWKNB99KSD50_Q&s=m:&恶魔之谜|这么晚饭都江堰水利设施方面的知识结构设计和建造于一些事情一些事情之后辣";
//        String decode = URLDecoder.decode(s, "UTF-8");
//        String decode1 = URLDecoder.decode(decode, "UTF-8");
//        System.out.println("decode " + decode);
//        System.out.println("decode1 " + decode1);

        //JsonTest();

       // listTst();

        AgloTest.test();
        DataStructTest.test();

        DisgnModeTest.test();
    }


    private final static int MAX_NUM = 1000000000;




    private static void listTst() {
        ZLinkList<Integer> stringZLinkList = new ZLinkList<>();

        for (int i = 0; i < MAX_NUM; i++) {
            stringZLinkList.add(i);
        }

        //stringZLinkList.print();
        long startTime = System.currentTimeMillis();
        int index = stringZLinkList.find(MAX_NUM - 1);
        long endTime = System.currentTimeMillis();

        System.out.println("startTime " + startTime + " endTime " + endTime + " escape " + (endTime - startTime));


        int[] values = new int[MAX_NUM];
        for (int j = 0; j < MAX_NUM; j++) {
            values[j] = j;
        }


        long startTime1 = System.currentTimeMillis();
        int i = find2(values, MAX_NUM - 1);
        long endTime1 = System.currentTimeMillis();

        System.out.println("startTime1 " + startTime1 + " endTime1 " + endTime1 + " escape1 " + (endTime1 - startTime1));


        //LinkedList
    }

    private static int find2(int[] values, int findValue) {
        int i = 0;
        int length = values.length;
        while (!(values[i] == findValue) && i < length) {
            i++;
        }
        return i;
    }


    private static void JsonTest() {
        String json = "{name:\"jason\",father:\"jason\",age:18}";
        //name:"jason"
        //age:18
        //\"\\w+\" 字符串属性
        Pattern p = Pattern.compile("\\w+:(\"\\w+\"|\\d*)");
        Matcher m = p.matcher(json);
        while (m.find()) {
            String text = m.group();
            System.out.println(" text -> " + text);
            int dotPos = text.indexOf(":");
            String key = text.substring(0, dotPos);
            String value = text.substring(dotPos + 1, text.length());
            //替换字符串的开始结束的双引号
            value = value.replaceAll("^\\\"|\\\"$", "");
            System.out.println(key + " -> " + value);

        }


    }




    //字符占用测试
    private static void testCharset() throws UnsupportedEncodingException {
        String[] charsetArr = {"gbk", "utf-8", "utf-16", "gb2312"};
        String[] strArr = {"汉子", "三", "1", "b"};
        for (String str : strArr) {
            System.out.println("");
            for (String charset : charsetArr) {
                System.out.println("测试字符：" + str + "\t编码：" + charset
                        + "\t所占字节数：" + str.getBytes(charset).length);
            }
        }

    }




    private static final ThreadLocal<Integer> value = new ThreadLocal<Integer>() {
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    static class MyThread implements Runnable {
        private String index;

        public MyThread(String index) {
            this.index = index;
        }

        //局部变量
        private Integer num = 0;

        public void run() {
            System.out.println("线程" + Thread.currentThread().getName() + index + "的初始 num:" + num);
            System.out.println("线程" + Thread.currentThread().getName() + index + "的初始 value:" + value.get());
            for (int i = 0; i < 10; i++) {
                num += i;
                value.set(value.get() + i);
            }
            System.out.println("线程" + Thread.currentThread().getName() + index + "的累加 num:" + num);
            System.out.println("线程" + Thread.currentThread().getName() + index + "的累加 value:" + value.get());
        }
    }
}
